/***** cfchl v1.01.00 By 陈发 *****/
;(function(global){
	"use strict";
	var M = function() {
		this.elem = '.cfchl';
		this.title = 'code';
		this.lang = 'default';
	};
	M.prototype = {
		//设置基础信息
		render : function(json) {
			this.elem = json.elem || '.cfchl';
			this.title = json.title || 'code';
			this.lang = json.lang || 'default';
		},
		//主函数
		run : function() {
			var dom = this._dom()[0];
			var type = this._dom()[1];
			var co;
			if (0 == type) {
				for(var i = 0; i < dom.length; i++) {
					this.encode = dom[i].encode || this.encode;
					this.title = dom[i].title || this.title;
					this.lang = dom[i].lang || this.lang;
					co = dom[i].innerHTML;
					co = this._code(co);
					dom[i].innerHTML = co;
				}
			} else {
				this.encode = dom.encode || this.encode;
				this.title = dom.title || this.title;
				this.lang = dom.lang || this.lang;
				co = dom.innerHTML;
				co = this._code(co);
				dom.innerHTML = co;
			}
			this._loadStyle();
		},
		//获取DOM
		_dom : function() {
			var records = [];
			var flag=this.elem.substr(0, 1);
			var value=this.elem.substr(1);
			if('.'==flag){
				records[0] = document.getElementsByClassName(value);
				records[1] = 0;
			}else{
				records[0] = document.getElementById(value);
				records[1] = 1;
			}
			return records;	
		},
		//组装code;
		_code : function(code) {
			var tmpCode = code;
			switch(this.lang) {
				case 'css' :
					tmpCode  = this._css(tmpCode);
					break;
				case 'html' :
					tmpCode  = this._html(tmpCode);
					break;
				default:
					break;
			};
			var newCode = '';
			newCode += '<div>';
			newCode += '<h4>'+this.title+'</h4>';
			newCode += '<ol>';
			newCode += '<li>';
			newCode += '<span>';
			newCode += tmpCode;
			newCode += '</span>';
			newCode += '</li>';
			newCode += '</ol>'
			newCode += '</div>';
			newCode  = newCode.replace(/\n/g, '</span></li><li><span>');
			return newCode;
		},
		//定义外观
		_style : function() {
			return '' +
				this.elem+'{display:block; box-sizing:border-box; -moz-box-sizing:border-box; -webkit-box-sizing:border-box; margin:10px 0;  border:solid 1px #cccccc; border-radius:4px; font-family:"Courier New", "consolas"; white-space:pre-line; color:#000000}' +
				this.elem+' h4{margin:0; padding:5px 15px; background:#F5F5F5}'+
				this.elem+' ol{margin:0; font-size:13px}' +
				this.elem+' ol li{line-height:1.39; background:#F5F5F5; color:#808080; padding-left:10px; padding-right:10px}'+
				this.elem+' span{color:#000000}' +
				this.elem+' .red{color:#ff0000}' +
				this.elem+' .dark-orange{color:#843900}' +
				this.elem+' .blue{color:#0000EE}' +
				this.elem+' .green{color:#008800}' +
				this.elem+' .gray{color:#808080}';
		},
		//加载样式表
		_loadStyle : function() {
			var style = document.createElement("style");
			var head = document.head || document.getElementsByTagName('head')[0];
			style.type = "text/css";
			style.rel = 'stylesheet';
			style.innerHTML = this._style();
			head.appendChild(style);
		},
		//css高亮显示
		_css : function(c) {
			var newCode = c;
			newCode = newCode.replace(/(}?\s*\n*\s*|,?\s*\n*\s*)([^;,}{+\/\(\)&gt;]+|[^;,}{+\/\(\)>]+)(\s*\n*\s*>;|\s*\n*\s*&gt;|\s*\n*\s*\+|\s*\n*\s*,|\s*\n*\s*{|\s*\n*\s*\()/g, '$1<span class="dark-orange">$2</span>$3');//处理标签
			newCode = newCode.replace(/(\/\*)([\s\S]*?)(\*\/)/g, function(w, $1, $2, $3){
				var replaceCode = $2.replace(/(\n+)/g, '<\/span>$1<span class="green">');
				return '<span class="green">/*'+replaceCode+'*/</span>';
			});//处理注释
			newCode = newCode.replace(/({\s*\n*|;\s*\n*|\(\s*)([^:{,]+)(\s*:)/g,'$1<span class="red">$2</span>$3');//处理属性名
			newCode = newCode.replace(/(:\s*)([^;}){]*)(\s*;|\s*}|(\s*)|\s*;?\s*})/g, '$1<span class="blue">$2</span>$3');//处理属性值
			return newCode;
		},
		_js : function(c) {
			var newCode = c;
			return newCode;
		},
		//html高亮显示
		_html : function(c) {
			var _this = this;
			var newCode = c;
			//处理标签
			//内联样式表也属于HTML标签的属性，按照HTML标签属性处理，不进行单独处理
			newCode = newCode.replace(/(<\/?|&lt;\/?)([^\!][^\n>]*|[^\!][^\n&gt;]*)(\/?>|\/?&gt;)/g, function(w, $1, $2, $3){
				var c1 = '<span class="blue">'+$1.replace(/</g, '&lt;')+'</span>';
				var c3 = '<span class="blue">'+$3.replace(/>/g, '&gt;')+'</span>';
				var c2 = '<span class="dark-orange">'+$2+'</span>';
				if (-1 != $2.indexOf(' ')) {
					var c2 = $2.replace(/([^\s]+)(\s[^=]+=".*")/g, function(w, $1, $2){
						var c4 = '<span class="dark-orange">'+$1+'</span>';
						var c5 = $2.replace(/(\s[^=]+)(='[^'"]*'|="[^'"]*")/g, function(w, $1, $2){
							var c6 = '<span class="red">'+$1+'</span>';
							var c7 = '<span class="blue">'+$2+'</span>';
							return c6+c7;
						});
						return c4+c5
					});
				}
				return c1 + c2 + c3;
			});
			newCode = newCode.replace(/(&lt;!DOCTYPE.*&gt;|&lt;!DOCTYPE.*>)/g, '<span class="gray">$1</span>');//处理声明
			//处理注释
			newCode = newCode.replace(/(<|&lt;)(!---*[\s\S]*?--)(>|&gt;)/g, function(w, $1, $2, $3){
				var c1 = $1.replace(/</g, '&lt;');
				var c2 = $2.replace(/(\n+)/g, '<\/span>$1<span class="green">');
				var c3 = $3.replace(/>/g, '&gt;');
				return '<span class="green">'+c1+c2+c3+'</span>';
			}); 
			//处理内部CSS
			newCode = newCode.replace(/(e">style.*&gt;<\/span>)([^<]*)(<.*style)/g, function(w, $1, $2, $3){
				var c1 = _this._css($2);
				return $1+c1+$3;
			});
			return newCode;
		}
	};
	global.cfchl = M;
})(this);